package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.NoticeMsgBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class NoticeMessVM extends TitleVM {
    public NoticeMessVM(@NonNull Application application, BaseModel model) {
        super(application, model);
    }

    public ObservableField<String> msg = new ObservableField<>();
    public ObservableField<String> msgTxt = new ObservableField<>();

    public void getMsg(String s){
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("CopyType",s);
        model.copywriting(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<NoticeMsgBean>() {
                    @Override
                    public void onNext(NoticeMsgBean o) {
                        if(o.issucc){
                            if (o.getData().contains("小区名称")){
                                String s = o.getData().replace("小区名称",msg.get());
                                msgTxt.set(s);
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand copy = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label",msgTxt.get());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            ToastUtils.showShort("复制成功");
        }
    });
}
